package com.incrediblesoft.redjey.demo;

import com.incrediblesoft.redjey.event.ActionEvent;
import com.incrediblesoft.redjey.event.ActionListener;
import com.incrediblesoft.redjey.*;
import com.incrediblesoft.redjey.layouts.GridLayout;

import javax.microedition.lcdui.TextBox;

public class ComponentsPage extends Frame implements ActionListener
{
    Window homeWindow;
    Menu demoMenu;
    MenuItem goHomeItem;

    public ComponentsPage(Window homeWindow)
    {
        super("This is a frame");
        this.homeWindow = homeWindow;

        /**
         * First I will want to customize some styles
         * I will add images instead of built-in rectangles
         * for checkboxes and radio buttons.
         * But this time I will not use methods.
         */
        String styleDesc = "MenuItem extends MenuItem { stick-image: image:{ name: /process-working.png; frame-width: 16; frame-height: 16; framerate: 10;}; }"+
                           "MenuItem:pressed extends MenuItem:pressed { stick-image: image:{ name: /process-working.png; frame-width: 16; frame-height: 16; framerate: 10;}; }"+
                           "Menu:normal extends MenuItem:normal {}"+
                           "Menu:pressed extends MenuItem:pressed {}";

        UIManager.loadStyles(styleDesc, true);

        Container contentPane = getContentPane();

        contentPane.add(new Label("1. This is a simple label"));
        contentPane.add(new Label("Label").setAlignment(HCENTER_ALIGNMENT));
        contentPane.add(new Label("2. This is a simple button"));
        contentPane.add(new Button("Iconless Button").setAlignment(HCENTER_ALIGNMENT));
        contentPane.add(new Button("Icon button", new CustomImage(UIManager.getImage("/gnome.png"))).setAlignment(HCENTER_ALIGNMENT));

        contentPane.add(new Label("3. TextPane"));
        String text = "I can see what you see not,\nVision milky then eyes rot,\nWhen you turn they will begun,\nWhispering their hidden song\nThen you'll see what cannot be,\nShadows move where light should be,\nOut of darkness, out of mind,\nCast down into The Halls of the Blind.";
        TextPane textPane = new TextPane(text);
        //Container cont = new Container();
        //cont.add(textPane);
        //cont.add(new Button("Hello"));
        //for (int i=0; i<5; i++)
        //    cont.add(new Button("Button "+(i+1)));

        ScrollPane textScrollPane = new ScrollPane(textPane);
        Dimension prefSize = textPane.getPreferredSize();
        textScrollPane.setPreferredSize(new Dimension(prefSize.width * 3 / 4, prefSize.height * 3 / 4));
        //textScrollPane.setAlignment(HCENTER_ALIGNMENT);
        //textScrollPane.setPreferredSize(new Dimension(100, 50));
        
        contentPane.add(textScrollPane);

        contentPane.add(new Label("4. Checkboxes"));
        contentPane.add(new RadioButton("Checker 1", true).setAlignment(HCENTER_ALIGNMENT));
        contentPane.add(new RadioButton("Checker 2", false).setAlignment(HCENTER_ALIGNMENT));

        contentPane.add(new Label("5. Radio group"));
        RadioButtonGroup group = new RadioButtonGroup();
        RadioButton radio1 = (RadioButton) contentPane.add(new RadioButton("Radio 1", true, "radio").setAlignment(HCENTER_ALIGNMENT));
        RadioButton radio2 = (RadioButton) contentPane.add(new RadioButton("Radio 2", false, "radio").setAlignment(HCENTER_ALIGNMENT));
        radio1.setRadioButtonGroup(group);
        radio2.setRadioButtonGroup(group);

        contentPane.add(new Label("6. Text field"));
        contentPane.add(new ScrollPane(new TextField("Title", "Some text", 128, javax.microedition.lcdui.TextField.ANY), ScrollPane.SCROLLBARS_ALWAYS));

        contentPane.add(new Label("7. Static image"));
        contentPane.add(new Button(null, new CustomImage(UIManager.getImage("/java.png")), "imageitem").setAlignment(HCENTER_ALIGNMENT));
        contentPane.add(new Label("8. Animated image"));
        CustomImage animation = new CustomImage(UIManager.getImage("/strip8bit.png"), 130, 80, 5);
        animation.setNumFrames(17);
        contentPane.add(new Button(null, animation, "imageitem").setAlignment(HCENTER_ALIGNMENT));

        contentPane.add(new Label("9. Custom Font"));
        CustomFont font = CustomFont.getFont("/font4.fnt");
        Label label = new Label("This is a simple label again");
        label.getCurrentStyle().setFont(font);
        contentPane.add(label.setAlignment(HCENTER_ALIGNMENT));

        Menu demoMenu = new Menu("Menu");
        Menu subMenu1 = new Menu("Submenu 1");
        Menu subMenu2 = new Menu("Submenu 2");
        for (int i=0; i<5; i++) subMenu2.add(new MenuItem("Item "+(i+1)));
        subMenu1.add(subMenu2);
        for (int i=0; i<5; i++) subMenu1.add(new MenuItem("Item "+(i+1)));
        for (int i=0; i<4; i++) demoMenu.add(new MenuItem("Item "+(i+1)));
        demoMenu.add(subMenu1);

        goHomeItem = new MenuItem("Go Home");
        goHomeItem.addActionListener(this);

        MenuBar menuBar = new MenuBar();
        menuBar.add(demoMenu);
        menuBar.add(goHomeItem);
        setMenuBar(menuBar);

        demoMenu.setPopupPlacement(Menu.RIGHT_PLACEMENT);
    }

    public void actionPerformed(ActionEvent e)
    {
        Component source = e.getSource();

        if (source == goHomeItem)
        {
            setVisible(false);
            homeWindow.setVisible(true);
        }
    }
}
